
LIBNAME alldat '~/jim/data';





*******  FAMILY FILES   **********;

filename fmly801 pipe 'gunzip -c ~/jim/data/ce8081/08423-0001-Data.txt.gz';
filename fmly802 pipe 'gunzip -c ~/jim/data/ce8081/08423-0005-Data.txt.gz';
filename fmly803 pipe 'gunzip -c ~/jim/data/ce8081/08423-0009-Data.txt.gz';
filename fmly804 pipe 'gunzip -c ~/jim/data/ce8081/08423-0013-Data.txt.gz';

filename fmly811 pipe 'gunzip -c ~/jim/data/ce8081/08423-0017-Data.txt.gz';
filename fmly812 pipe 'gunzip -c ~/jim/data/ce8081/08423-0021-Data.txt.gz';
filename fmly813 pipe 'gunzip -c ~/jim/data/ce8081/08423-0025-Data.txt.gz';
filename fmly814 pipe 'gunzip -c ~/jim/data/ce8081/08423-0029-Data.txt.gz';
filename fmly815 pipe 'gunzip -c ~/jim/data/ce8081/08423-0033-Data.txt.gz';

filename home841 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0003-Data.txt.gz';
filename home842 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0007-Data.txt.gz';
filename home843 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0011-Data.txt.gz';
filename home844 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0015-Data.txt.gz';
filename home845 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0019-Data.txt.gz';

%include "~/jim/poverty/include/infile_cons.mcr";
%MACRO read(qyr,statement);
 
  DATA famly&qyr;
   INFILE fmly&qyr lrecl=3400;
   INPUT &statement;
   IF &qyr IN (815,855,955,1045) THEN ex_yr=1;
     ELSE ex_yr=0;
   srv_year=FLOOR(&qyr/10)+ex_yr;
   qyear=&qyr;
   newid=9000000+newid;
  RUN;
 
  PROC MEANS; 
  RUN; 
 
%MEND read; 

%read(801,&all8081);
%read(802,&all8081); 
%read(803,&all8081);
%read(804,&all8081); 
 
%read(811,&all8081); 
%read(812,&all8081); 
%read(813,&all8081); 
%read(814,&all8081);
%read(815,&all8081); 

DATA cexdat;
MERGE
famly801 famly802 famly803 famly804
famly811 famly812 famly813 famly814 famly815;
BY newid;

KEEP newid propvalu;
RUN;


  
****************************************
READ IN DATA
****************************************;

filename home841 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0003-Data.txt.gz';
filename home842 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0007-Data.txt.gz';
filename home843 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0011-Data.txt.gz';
filename home844 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0015-Data.txt.gz';
filename home845 pipe 'gunzip -c ~/jim/data/ce_mtab/1984/08671-0019-Data.txt.gz';

***************  84:1  *************************************************;

DATA hm841;  
INFILE home841;
INPUT newid 1-8 ucc 9-14 propvalu 15-26;
IF ucc=800721;

newid=9000000+newid;
RUN;

***************  84:2  *************************************************;

DATA hm842;  
INFILE home842;
INPUT newid 1-8 ucc 9-14 propvalu 15-26;
IF ucc=800721;

newid=9000000+newid;
RUN;

***************  84:3  *************************************************;

DATA hm843;  
INFILE home843;
INPUT newid 1-8 ucc 9-14 propvalu 15-26;
IF ucc=800721;

newid=9000000+newid;
RUN;

***************  84:4  *************************************************;

DATA hm844;  
INFILE home844;
INPUT newid 1-8 ucc 9-14 propvalu 15-26;
IF ucc=800721;

newid=9000000+newid;
RUN;

***************  84:5  *************************************************;

DATA hm845;  
INFILE home845;
INPUT newid 1-8 ucc 9-14 propvalu 15-26;
IF ucc=800721;

newid=9000000+newid;
RUN;


*******************************************************************
GET 84 HOME DATA TO IMPUTE RENTEQ FOR 80-81
*******************************************************************;
DATA home;
   SET hm841 hm842 hm843 hm844 hm845;

   numhous=1;
   *prop values are at monthly level, so multiply by 4 and sum across the 3 months;
   propvalu=propvalu*4;

RUN;

PROC SORT DATA=home;
BY newid;
PROC MEANS SUM NOPRINT;
     BY newid;
     VAR numhous propvalu;
     OUTPUT OUT=out_h(DROP=_FREQ_ _TYPE_) SUM=n_hous totpval;
RUN;


PROC SORT DATA=alldat.cons_final60_12;
   BY newid;
RUN;

DATA cons_debt_data;
   MERGE alldat.cons_final60_12(KEEP=qyear cons1-cons9 ref_year newid wgt20 educ_ref ftype ref_race region 
		     bls_urbn incweek1 incweek2 earn_comp hispanic age_ref ed_type sex_ref sex scale
		     fdhomepq2 fdhomecq2 rendwepq rendwecq utilpq utilcq hflow tflow2 tflow
		     d_fdhome d_rent d_utility fdhome totrentdwe Utilites
		     ckbkactx_ ckbkactx secestx_ secestx usbndx_ usbndx savacctx_ savacctx assets72 
				 tot_debt2 rt1 srv_year respstat cutenure transpq transcq
				 n_tot_vflow1 vehfinpq vehfincq vehicpq vehiccq cartknpq cartkncq 
				 cartkupq cartkucq caropq carocq 
				 perslt18 fam_size fincbtax occ_recode topcode_inc earncomp d_gas gasoline_oil
				 d_othvehic d_autofin d_pubtrans d_owndwell houseval married educ_code
				 automobileops Publictransother totexp2 totowndwe
				 gasmopq gasmocq 
				 state1 totexp fam_type marital1   
        rooms publhous govtcost stove microw frig freezer disposal dishw
        win_ac cen_ac washer dryer bedroom bathrm hlfbath smsastat intvmo
        renteq owndwepq owndwecq totexp locat vehq IN=in1)
        out_h(KEEP=newid totpval n_hous)
        cexdat;
   BY newid;
   IF in1;

IF 801 LE qyear LE 815 THEN totpval=propvalu; 
   
*IF ref_year GE 1980;

IF 59 LE srv_year LE 62 THEN gas_mo=gasoline_oil/4;
   ELSE IF 71 LE srv_year LE 74 THEN gas_mo=d_gas/4;
   ELSE IF srv_year GE 80 THEN gas_mo=SUM(OF gasmopq gasmocq);


RUN;

***********************************************************************
MERGE IN UTILITIES DATA
***********************************************************************;
PROC SORT DATA=cons_debt_data;
  BY qyear newid;
RUN;
PROC SORT DATA=alldat.utilities80_12;
  BY qyear newid; 
RUN;

DATA cons_debt_data;
   MERGE cons_debt_data(IN=in1) alldat.utilities80_12;
   BY qyear newid; 
   IF in1;
RUN;

PROC MEANS;
RUN;
PROC SORT;
  BY srv_year; 
RUN;
PROC MEANS;
  BY srv_year; 
RUN;


proc export data=cons_debt_data 
	OUTFILE="~/jim/data/data_for_others/inequality_data_for_mike_murto.dta"
	REPLACE;
run;


ENDSAS;

